package sad.visao;

import java.awt.Color;
import javax.swing.JOptionPane;
import sad.util.DAOEvent;

public class CadAlternativaDialog extends DialogAbstrato {
    /*Classe responsavel pelo cadastro ou edição do professor
    ela é instanciada apartir da ação do obj btProfessor ou do novoProfessor
    na classe SadGui*/

    public CadAlternativaDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveAlternativa = new javax.swing.JPanel();
        Enunciado = new javax.swing.JTextField();
        lblEnunciado = new javax.swing.JLabel();
        lblMessage = new javax.swing.JLabel();
        resp = new javax.swing.JComboBox();
        btnCadastrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SAD - Cadastro");

        saveAlternativa.setBorder(javax.swing.BorderFactory.createTitledBorder("Salvar Alternativa"));

        Enunciado.setFocusCycleRoot(true);
        Enunciado.setFocusTraversalPolicyProvider(true);

        lblEnunciado.setText("Enunciado:");

        resp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Verdadeiro", "Falso" }));
        resp.setToolTipText("");

        javax.swing.GroupLayout saveAlternativaLayout = new javax.swing.GroupLayout(saveAlternativa);
        saveAlternativa.setLayout(saveAlternativaLayout);
        saveAlternativaLayout.setHorizontalGroup(
            saveAlternativaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, saveAlternativaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(saveAlternativaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(Enunciado, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                    .addComponent(lblEnunciado, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMessage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                    .addComponent(resp, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        saveAlternativaLayout.setVerticalGroup(
            saveAlternativaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveAlternativaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblEnunciado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Enunciado, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(resp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(lblMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        btnCadastrar.setText("Cadastrar");
        btnCadastrar.setActionCommand("Salvar Professor");

        btnCancelar.setText("Cancelar");
        btnCancelar.setActionCommand("Cancelar Professor");

        btnExcluir.setText("Excluir");
        btnExcluir.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveAlternativa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                    .addComponent(btnExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(saveAlternativa, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        saveAlternativa.getAccessibleContext().setAccessibleName("");
        btnExcluir.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Enunciado;
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JLabel lblEnunciado;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JComboBox resp;
    private javax.swing.JPanel saveAlternativa;
    // End of variables declaration//GEN-END:variables

    // <editor-fold defaultstate="collapsed" desc="Responde a eventos DAO">
    @Override
    public void modelDAOChange(DAOEvent evt) {
        switch (evt.getID()) {
            case DAOEvent.DAO_CREATE:
                resetDialog();
                lblMessage.setForeground(Color.GREEN);
                lblMessage.setText("Alternativa"
                        + " cadastrada com sucesso!!!");
                break;

            case DAOEvent.DAO_EXISTE:
                JOptionPane.showMessageDialog(this, "A Alternativa "
                        + evt.getField() + " já existe!!!", null,
                        JOptionPane.WARNING_MESSAGE);
                resetDialog();
                break;

            case DAOEvent.DAO_DELETE:
                lblMessage.setForeground(Color.GREEN);
                lblMessage.setText("Alternativa"
                        + " deletada com sucesso!!!");
                break;


            case DAOEvent.DAO_ERROFIELD:
                JOptionPane.showMessageDialog(this, "O campo " + evt.getField()
                        + " não pode ficar em branco!!!");
                focusField(evt.getField());
                break;
        }
    }// </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Retorna dados do objeto">

    public String getEnunciado() {
        return this.Enunciado.getText();
    }

    public boolean getResposta() {
        String resposta =  (String) resp.getSelectedItem();
        if (resposta.equalsIgnoreCase("Verdadeiro")){
            return true;
        } else
            return false;
    }// </editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Métodos públicos comuns a todos os Dialogs">
    @Override
    public void addOuvintes() {
        btnCadastrar.addActionListener(getSuporteEventos());
        btnExcluir.addActionListener(getSuporteEventos());
        btnCancelar.addActionListener(getSuporteEventos());
        this.addWindowOuvinte(getSuporteEventos());
    }

    @Override
    public void close() {
        this.resetDialog();
        this.dispose();
    }

    @Override
    public void setEditableDados(Object[] o) {
        if (o != null) {
            Enunciado.setText(o[0].toString());
            txtLogin.setText(o[1].toString());
            txtLogin.setEnabled(false);
            txtSenha.setText(o[2].toString());
            btnExcluir.setVisible(true);
            btnCadastrar.setText("Alterar");
            btnCadastrar.setActionCommand("Alterar");
            saveAlternativa.setBorder(
                    javax.swing.BorderFactory.createTitledBorder("Alterar Professor"));
            btnCancelar.setText("Voltar");
        }
    } //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Métodos privados, mas comuns a todos os Dialogs">
    @Override
    protected void resetDialog() {
        Enunciado.setText(null);
        txtLogin.setText(null);
        txtSenha.setText(null);
        lblMessage.setText(null);
        Enunciado.requestFocusInWindow();
    }

    @Override
    protected void focusField(String field) {
        if (field.equals("Nome")) {
            Enunciado.requestFocusInWindow();
        }
        if (field.equals("Login")) {
            txtLogin.requestFocusInWindow();
        }
        if (field.equals("Senha")) {
            txtSenha.requestFocusInWindow();
        }
    }
    //</editor-fold>
}
/*Pelo que eu to entendendo está sendo instanciado mais de um suporteEvento*/